<script>
    var vm = new Vue({
        el: '#app',
        data:<?php echo Building::getInstance()->renderForm('form',$fields,$validate,$extra);?>,
        methods: {
            submitForm:function(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(vm.markdown == true){
                            eval(vm.markdown_eval)
                        }
                        $.post('/<?php echo $controller."/post"?>', vm.form).done(function(response) {
                            if(200 == response.code){
                                vm.form = <?php echo Building::getInstance()->renderClearForm($fields)?>;
                                vm.$message.success(response.msg);
                                let is_reload = <?php echo $reload?:0?>;
                                if(is_reload){
                                    window.location.reload();
                                }else{
                                    vm.init();
                                }
                                vm.dialog = false
                            }else{
                                vm.$message.error(response.msg);
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            handleSuccess(response, file) {
                if(response.code == 200){
                    eval(response.data.path)
                }else{
                    vm.$message.error(response.msg);
                }
            },
            saveForm:function (idx,row) {
                vm.form = row
                vm.dialog = true
                if(vm.markdown == true){
                    vm.editor()
                }
            },
            deleteForm:function (idx,row) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
                    $.post('/<?php echo $controller."/delete"?>', {id:row.id}).done(function(response) {
                        if(200 == response.code){
                            vm.$message.success(response.msg);
                            vm.init();
                        }else{
                            vm.$message.error(response.msg);
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            },
            init:function (page) {
                $.post('/<?php echo $controller."/index"?>', {page:page}).done(function(response) {
                    if(200 == response.code){
                        vm.table = response.data.list
                        vm.total = response.data.total
                    }else{
                        vm.$message.error(response.msg);
                    }
                })
            },
            //开启markdown
            editor:function () {
                vm.dialog = true
                setTimeout(function () {
                        let edit = editormd("editor_content", {
                            width  : "100%",
                            height : "500px",
                            path   : "/static/editor.md/lib/",
                            emoji:false,
                            tocm:true,
                            flowChart:true,
                            sequenceDiagram:true,
                            imageUpload:true,
                            imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                            imageUploadURL : "/upload/editor",
                        });
                        vm.editors = edit
                },300)
            }
        },
        mounted:function () {
            this.init(0);
        },
    })
</script>